package Ants.Model;

import java.util.Comparator;
import java.util.List;

import Ants.Actuator;

public class Ant implements  Comparable<Ant> {

	private Tile tile;
	private Tile preMove;
	private boolean busy;
	private boolean isThreatened;
	private int id;
	private List<Ant> threatenEnemies;
	
	public List<Ant> getThreatenEnemies() {
		return threatenEnemies;
	}

	public void setThreatenEnemies(List<Ant> threatenEnemies) {
		this.threatenEnemies = threatenEnemies;
	}

	public boolean isThreatened() {
		return isThreatened;
	}

	public void setThreatened(boolean isThreatened) {
		this.isThreatened = isThreatened;
	}

	public void setBusy(boolean busy) {
		this.busy = busy;
	}

	public Ant(Tile tile){
		this.tile = tile;
	}

	public Tile getTile() {
		return tile;
	}

	public void setTile(Tile tile) {
		this.tile = tile;
	}

	public boolean isBusy() {
		return busy;
	}

	public void moveTo(Tile adjacentTile, Status status){
		//La tile deve essere adiacente a quella attuale per essere una mossa valida
		//quindi stessa riga o stessa colonna

		int mapRows = Status.Rows;
		int mapCols = Status.Cols;

		//Se l'ho mossa
		if(tile != adjacentTile){
			
			if(tile.getRow() == adjacentTile.getRow()){
				if(((tile.getCol()+1)%mapCols)==adjacentTile.getCol())
					Actuator.DoAction(tile.getRow(), tile.getCol(), "E");
				else
					Actuator.DoAction(tile.getRow(), tile.getCol(), "W");
			}
			else{
				if(((tile.getRow()+1)%mapRows)==adjacentTile.getRow())
					Actuator.DoAction(tile.getRow(), tile.getCol(), "S");
				else
					Actuator.DoAction(tile.getRow(), tile.getCol(), "N");
			}

			tile.setType(Type.LAND);
			tile.setAnt(null);

			if(tile.isChecked()){
				tile.setChecked(false);
				adjacentTile.setChecked(true);
			}

			adjacentTile.setType(Type.MY_ANT);
			adjacentTile.setAnt(this);

			preMove = tile;
			tile = adjacentTile;
		}
		else{
			preMove = tile;
		}

		busy = true;
		status.addMovedAnts(preMove, this);
	}

	public Tile getPreMove() {
		return preMove;
	}

	@Override
	public String toString() {
		return tile.toString();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public int compareTo(Ant o) {
		if(threatenEnemies.size() > o.threatenEnemies.size()){
			return 1;
		}				
		else if(threatenEnemies.size() == o.threatenEnemies.size()){
			return 0;
		}				
		else{
			return -1;
		}	
	}

}
